<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>对称数</title>
  </head>
  <body>
    <script>
      //第 81 题：打印出 1 - 10000 之间的所有对称数 例如：121、1331 等
      console.log('打印对称数')
      function isDC(num) {
        let s = num + '';
        if (s.length <= 1) return false;
        let length = s.length;
        let isTr = true;
        for (let i = 0; i < Math.ceil(length / 2); i++) {
          if (s[i] !== s[length - 1 - i]) {
            isTr = false;
          }
        }

        return isTr;
      }
      let total = 0
      let arr = []
      for (let i = 0; i < 100000; i++) {
        if (isDC(i)) {
          total += 1;
          arr.push(i);
          // console.log(i);
        }
      }
      console.log('total: ', total);
      console.log('arr: ', arr);

      //
      ;[...Array(10000).keys()].filter((x) => x.toString().length > 1 && x === Number(x.toString().split('').reverse().join('')));
      //
      ;[...Array(10000).keys()].filter( x => x.toString().length > 1 && x === Number([...x.toString()].reverse().join('')));

      //
      const isSymmetryNumber = n => n > 10 && `${n}` === [...`${n}`].reverse().join('');
      const getSymmetryNumber = num => Array.from({ length: num }, (n, i) => i + 1).filter(isSymmetryNumber);


      // 以下适用10000以内
      ;[...Array(10000).keys()].filter(v => {
        return v === Number((String(v).match(/(^\d)(\d)?\2*\1$/) || [])[0])
      })

      //精简一下
      ;[...Array(10000).keys()].filter(v => {
        return (String(v).match(/(^\d)(\d)?\2*\1$/) || [])[0]
      })

      //再精简一下
      ;[...Array(1e4).keys()].filter(v => (v + '').match(/(^\d)(\d)?\2*\1$/))
    </script>
  </body>
</html>
